package monster.com.cvh.fragment;

import android.text.InputFilter;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.bean.PostBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.ResgisterButton;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/4/10
 * @function:
 */

public class ResetPasswordFragment extends monster.com.cvh.fragment.base.BaseFragment {
    @BindView(R.id.edt_activity_forget_password_phone)
    EditText mEdtForgetPasswordPhone;
    @BindView(R.id.edt_activity_forget_password_code)
    EditText mEdtForgetPasswordCode;
    @BindView(R.id.edt_activity_forget_password_new_password)
    EditText mEdtForgetNewPassword;
    @BindView(R.id.edt_activity_forget_password_see)
    ImageView mIvForgetPasswordSee;
    @BindView(R.id.tv_activity_forget_password_get_code)
    TextView mTvGetCode;

    private Boolean showPassword = true;
    private Gson gson;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_reset_password;
    }

    @Override
    protected void initData() {
        gson = new Gson();
    }

    @Override
    protected void initEvent() {
        //限制手机号码字数为11位
        mEdtForgetPasswordPhone.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
    }


    @OnClick({
            R.id.iv_nomal_back, R.id.edt_activity_forget_password_see, R.id.tv_activity_forget_password_get_code,
            R.id.tv_activity_forget_password_comfirm
    })
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_nomal_back:
                getFragmentManager().popBackStack();
                break;
            case R.id.edt_activity_forget_password_see:
                //显示密码
                if (showPassword) {
                    mIvForgetPasswordSee.setImageResource(R.mipmap.login_see);
                    mEdtForgetNewPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mEdtForgetNewPassword.setSelection(mEdtForgetNewPassword.getText().toString().length());
                    showPassword = !showPassword;
                } else {
                    //隐藏密码
                    mEdtForgetNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mEdtForgetNewPassword.setSelection(mEdtForgetNewPassword.getText().toString().length());
                    mIvForgetPasswordSee.setImageResource(R.mipmap.login_notsee);
                    showPassword = !showPassword;
                }
                break;
            case R.id.tv_activity_forget_password_get_code:
                //获取验证码
                Log.e("lixiaofei", "我点击了获取验证码:" );
                ResgisterButton button = new ResgisterButton(mTvGetCode, 60000, 1000);
                button.start();
                if (mEdtForgetPasswordPhone.getText().toString() == null || mEdtForgetPasswordPhone.getText().toString().isEmpty()) {
                    showToastLong("请输入手机号码");
                } else {
                    OkGo.<String>post(MyConstant.SMS_CODE_URL).params("phone",mEdtForgetPasswordPhone.getText().toString().trim()).execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            PostBean postBean = gson.fromJson(s, PostBean.class);
                            switch (postBean.getCode()){
                                case 1:
                                    //请求成功
                                    showToastLong("获取验证码成功");
                                    break;
                                case -1004:
                                    showToastLong("请输入手机号码");
                                    break;
                                case 10:
                                    showToastLong("手机号码填写错误");
                                    break;
                            }
                        }
                    });
                }
                break;
            case R.id.tv_activity_forget_password_comfirm:
                if (mEdtForgetPasswordPhone.getText().toString() == null || mEdtForgetPasswordPhone.getText().toString().isEmpty()) {
                    showToastLong("请输入手机号码");
                } else if (mEdtForgetPasswordCode.getText().toString() == null || mEdtForgetPasswordCode.getText().toString().isEmpty()) {
                    showToastLong("请输入验证码");
                } else if (mEdtForgetNewPassword.getText().toString() == null || mEdtForgetNewPassword.getText().toString().isEmpty()) {
                    showToastLong("请输入密码");
                } else {
                    OkGo.<String>post(MyConstant.PWDRESET).params("smsCode",mEdtForgetPasswordCode.getText().toString().trim()).params("pwd",mEdtForgetNewPassword.getText().toString().trim())
                            .params("phone",mEdtForgetPasswordPhone.getText().toString().trim()).execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            PostBean pwdResetBean = gson.fromJson(s, PostBean.class);
                            switch (pwdResetBean.getCode()){
                                case 1:
                                    getFragmentManager().popBackStack();
                                    showToastLong("修改密码成功");
                                    break;
                                case -1004:
                                    showToastLong("参数缺失");
                                    break;
                                case 2:
                                    //账号长度异常
                                    showToastLong("手机号码填写错误");
                                case 10:
                                    showToastLong("无效的手机号码");
                                    break;
                                case 19:
                                    showToastLong("验证码输入错误");
                                    break;
                                case 21:
                                    //密码与上次相同 或者手机号未注册
                                    showToastLong("该手机号未注册或者密码重复");
                                    break;

                            }
                        }
                    });
                }
                break;
        }
    }

}
